package com.java.asviteslkyy.flashlight;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Flashlight {

    static final int DELAY = 1000;
    static Timer timer = new Timer();

    Battery battery;
    int powerOfLamp;

    public Flashlight(int powerOfLamp) {
        this.powerOfLamp = powerOfLamp;
    }

    public static void main(String[] args) {

        Flashlight flashlight = new Flashlight(2);
        Batteries batteries = new Batteries();

        int inputInMenu = 0;
        do {
            inputInMenu = menu();
            switch (inputInMenu) {
                case 1:
                    flashlight.insertBattery(true);
                    break;
                case 2:
                    flashlight.insertBattery(false);
                    break;
                case 3:
                    batteries.showBatteries();
                    flashlight.battery = batteries.useoldBattery();
                    break;
                case 4:
                    flashlight.removeBattery(batteries);
                    break;
                case 5:
                    flashlight.turnOnLight();
                    break;
                case 6:
                    flashlight.turnOffLight();
                    break;
            }
        } while (inputInMenu != 7);
    }

    private boolean batteryAlreadyInserted() {
        if (this.battery != null) {
            return false;
        } else {
            return true;
        }
    }

    public static int menu() {
        int result = 0;
        print("Press 1 to insert Alkaline battery");
        print("Press 2 to insert standart battery");
        print("Press 3 to insert used battery");
        print("Press 4 to remove battery");
        print("Press 5 to turn on light");
        print("Press 6 to turn off light");
        print("Press 7 to exit from program");
        do {
            result = getInputInt();
            if (!validateInt(result, 7)) {
                print("Not valid input. Try again:");
            }
        } while (!validateInt(result, 7));
        return result;
    }

    public static int getInputInt() {
        Scanner sc = new Scanner(System.in);
        int result = sc.nextInt();
        return result;
    }

    public static boolean validateInt(int input, int max) {
        boolean validate = false;
        for (int index = 1; index <= max; index++) {
            if (input == index) {
                validate = true;
                continue;
            }
        }
        return validate;
    }

    public void turnOnLight() {
        if (!batteryAlreadyInserted()) {
            initTimer();
        } else {
            print("Battery not inserted. Please insert");
        }
    }

    public static void print(String text) {
        System.out.println(text);
    }

    public void turnOffLight() {
        print("Your flashlight turned off");
        timer.cancel();
    }

    public void removeBattery(Batteries batteries) {
        batteries.addUsedBattery(this.battery);
        this.battery = null;
    }

    public void insertBattery(boolean alkalineBattery) {
        if (batteryAlreadyInserted()) {
            this.battery = new Battery(alkalineBattery);
            print("You are inserted " + battery.name + " battery");
        } else {
            print("Your flashlight already have battery");
        }
    }

    public void initTimer() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                ++battery.timeInUse;
                double voltage = battery.takeEnergy();
                print(String.valueOf(voltage));
                if (voltage < powerOfLamp) {
                    print("Your flashlight battery is empty. Flashlight does not light ");
                    timer.cancel();
                }
            }
        }, DELAY, 1000);
    }
}
